package juc;

import java.util.concurrent.locks.ReentrantLock;

class STicket{
    private static int sum=20;

    private final ReentrantLock lock=new ReentrantLock(true);
    public  void saleTicket(){

        try {
            lock.lock();
            if (sum > 0) {

                System.out.println(Thread.currentThread().getName()+"卖出：1张，剩余"+--sum);
            }
        }finally {
            lock.unlock();
        }
    }
}
public class STest2 {
    public static void main(String[] args) {
        STicket ticket=new STicket();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<1000;i++){

                    ticket.saleTicket();
                }

            }
        },"A").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<10000;i++){

                    ticket.saleTicket();
                }
            }
        },"B").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<10000;i++){

                    ticket.saleTicket();
                }
            }
        },"C").start();
    }
}
